package com.whlilong.jxc.editor.support;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;

import com.whlilong.jxc.listener.IEditorListener;
import com.whlilong.jxc.listener.ITextKeyChangeListener;
import com.whlilong.jxc.model.Kstype;

/**
 * 客商类别editor编辑器
 * 
 * @author auzn
 * 
 */

public class MerchantTypeEditorSupport extends EditingSupport implements
		ITextKeyChangeListener {

	/**
	 * 当前第几列
	 */
	private int currentColumn;

	/**
	 * 是否第一次修改该单元格
	 */
	private boolean isFirst = true;

	/**
	 * 为单元格添加的editor
	 */
	private CellEditor editor;
	
	
	@SuppressWarnings("unchecked")
	public MerchantTypeEditorSupport(TableViewer viewer, int column,
			IEditorListener listener) {
		super(viewer);
		
		this.currentColumn = column;

		switch (column) {
		case 2:
		case 3:
			editor = new MerchantTypeTextKeyCellEditor(viewer.getTable(),
					listener, column, this);
			break;
		default:
			editor = null;
		}
	}

	@Override
	protected boolean canEdit(Object element) {
		return (currentColumn != 1);
	}

	@Override
	protected CellEditor getCellEditor(Object element) {
		return editor;
	}

	@Override
	protected Object getValue(Object element) {
		Kstype obj = (Kstype) element;
		switch (currentColumn) {
		case 2:
			String id = obj.getKstpid();
			if (isFirst) {
				((MerchantTypeTextKeyCellEditor) editor).setOldString(id);
				isFirst = false;
			}
			return id;
		case 3:
			return obj.getKstype();
		}
		return null;
	}

	@Override
	protected void setValue(Object element, Object value) {
		Kstype obj = (Kstype) element;
		switch (currentColumn) {
		case 2:
			obj.setKstpid(String.valueOf(value));
			break;
		case 3:
			obj.setKstype(String.valueOf(value));
			break;
		}
		getViewer().update(element, null);
	}

	@Override
	public void changeEvent() {
		if (editor instanceof MerchantTypeTextKeyCellEditor)
			((MerchantTypeTextKeyCellEditor) editor).releaseKey();
	}

	@Override
	public void modify() {
		isFirst = true;

	}

}
